package serverManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import db_manager.Dir;
import db_manager.DirManager;
import db_manager.Manager;
import db_manager.User;
import db_manager.UserManager;

/**
 * this class waits for users to install the program and enter their details into
 * the DB
 */
public class UpdateUser extends Thread
{
	ServerSocket serverSocket=null;
	Socket clientSocket=null;
	boolean serverIsUp;
	Manager manager;
	
	/**
	 * the constructor of this class
	 * @throws IOException
	 */
	public UpdateUser() throws IOException 
	{
		manager = Manager.getInstance();
		serverIsUp = true;
		serverSocket = new ServerSocket(4443);
	}

	/**
	 * the Thread execution function
	 */
	public void run()
	{
		PrintWriter out = null;
	    BufferedReader in = null;
	    String user_name, password;
	    File sessionFile = new File("C:\\Program Files\\m&l_dropbox_server\\hibernate.cfg.xml");
	    // Opening a new session factory - for connection with DB
	    SessionFactory sessionFactory = new Configuration().configure(sessionFile).buildSessionFactory();
		try
		{
			// as long as the server program is up keep running
			while(serverIsUp)
			{
				// waits for an update
				clientSocket=serverSocket.accept();
				//creating session for connecting to DB
				Session session = sessionFactory.openSession();
				
				out=new PrintWriter(clientSocket.getOutputStream(),true);
				in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
				user_name=in.readLine();
				password = in.readLine();
				// check if this user name already exists
				boolean exist = manager.isUserExist(user_name, session);
				//the case the user exist in the DB
				if (exist)
				{
					session.flush();
					out.println("false");
				}
				//the case the user doesn't exist in the DB
				else
				{
					User user = new User();
					user.setUser_name(user_name);
					user.setPassword(password);
					UserManager um = new UserManager(session);
					// updating the DB
					um.saveUser(user);
					session.flush();
					//creating session for connecting to DB
					Session session2 = sessionFactory.openSession();
					Dir dir = new Dir();
					dir.setDir_name("DropBox");
					dir.setUser_id(manager.checkUserId(session2, user_name));
					dir.setDir_id(manager.nextDirId(session2));
					DirManager dm = new DirManager(session2);
					// updating the DB
					dm.saveDir(dir);
					session2.flush();
					out.println("true");
				}
			}
		}
		catch(Exception e) {}
	}

	/**
	 * ending this Thread
	 */
	public void shutDownServer()
	{
		try
		{
			// the case that this socket is open
			if (clientSocket != null)
			{
				clientSocket.close();
			}
			// the case that this server socket is open
			if (serverSocket != null) 
			{
				serverSocket.close();
			}
		}
		catch(Exception e) {}
		serverIsUp = false;
	}
}
